define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'small.goods/index',
                    add_url: 'small.goods/add',
                    edit_url: 'small.goods/edit',
                    del_url: 'small.goods/del',
                    multi_url: '',
                }
            });
            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                sortName: 'sort',
                escape: false,
                pagination: false,
                search: false,
                // commonSearch: false,
                columns: [
                    [
                        {field: 'state', checkbox: true, },
                        {field: 'id', title: 'ID'},
                        {field: 'title', title: '商品名称',operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符', style: 'width:200px'},
                        {field: 'images', title:'商品图片', events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'price', title: '价格'},
                        {field: 'discountsprice', title: '优惠价'},
                        {field: 'stock', title: '库存数量'},//,align: 'left', formatter: Controller.api.formatter.stock
                        {field: 'sales', title: '销量'},
                        {field: 'rating', title: '评价人数'},
                        {field: 'review', title: '浏览人数'},
                        {field: 'is_recommend', title: '是否推荐',formatter:function (value) {
                                if(value == 1){
                                    return '<span class="text-success"><i class="fa fa-circle"></i> 推荐</span>';
                                }else{
                                    return '<span class="text-success"><i class="fa fa-circle"></i> 不推荐</span>';
                                }
                            },operate:false
                        },
                        {field: 'status', title: '状态',formatter:function (value) {
                                if(value == 1){
                                    return '<span class="text-success"><i class="fa fa-circle"></i> 发布</span>';
                                }else{
                                    return '<span class="text-success"><i class="fa fa-circle"></i> 禁止</span>';
                                }
                            },operate:false
                        },
                        {field: 'createtime', title: '发布时间',formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), events: Table.api.events.operate, formatter: function (value, row, index) {
                                return Table.api.formatter.operate.call(this, value, row, index, table);
                            }}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Form.api.bindevent($("form[role=form]"));
        },
        edit: function () {
            Form.api.bindevent($("form[role=form]"));
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                // stock: function (value, row, index) {
                //     return '<div class="input-group input-group-sm" style="width:100px;">' +
                //         '<input type="text" class="form-control input-stock" value="' + value + '">' +
                //         '<span class="input-group-btn input-group-stock">' +
                //         '<a  class="btn btn-default btn-stock bg-success">提交</a></span>' +
                //         '</div>';
                // },
                // ip: function (value, row, index) {
                //     return '<a class="btn btn-xs btn-ip bg-success"><i class="fa fa-map-marker"></i> ' + value + '</a>';
                // },
            },
            events: {
                stock: {
                    'click .btn-stock': function (e, value, row, index) {
                        console.log(e);
                        var options = $("#table").bootstrapTable('getOptions');

                        //这里我们手动将数据填充到表单然后提交
                        // $("#commonSearchContent_" + options.idTable + " form [name='ip']").val(value);
                        // $("#commonSearchContent_" + options.idTable + " form").trigger('submit');
                    }
                },
                operate: $.extend({
                    'click .btn-detail': function (e, value, row, index) {
                        Backend.api.open('auth.adminlog/detail/ids/' + row['id'], __('Detail'));
                    }
                }, Table.api.events.operate)
            }
        },

    };
    return Controller;
});